package com.interauto.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 一些请求用到的统一工具处理
 */
public class HttpAutoUtils {
    public static String getCookievalue(Map<String,String> cookiemap){
        if(cookiemap == null || cookiemap.isEmpty()){
            return "";
        }
        StringBuilder builder=new StringBuilder();
        for(String headerkey:cookiemap.keySet()){
            builder.append(headerkey).append("=").append(cookiemap.get(headerkey)).append(";");
        }
        return builder.toString();
    }

    public static Map<String,String> getCookievaluetoMap(String cookiestr){
        if(StringUtils.isEmpty(cookiestr)){
            return null;
        }
        String[] kvastr=cookiestr.split(";");
        if(kvastr.length<1){
            return null;
        }
        Map<String,String> cookiemap=new HashMap<>();
        for(String kva:kvastr){
            String[] kvs=kva.split("=");
            if(kvs.length<1){
                continue;
            }
            cookiemap.put(kvs[0].trim(),kvs[1].trim());
        }
        return cookiemap;
    }


    /**
     * 将json转换成map。
     * @param headerstr 要处理的header json
     * @return headermap
     */
    public static Map<String,String> getHeaderMap(String headerstr){
        if(StringUtils.isEmpty(headerstr)){
            return null;
        }
        Map<String,String> headmap=new HashMap<>();
        try {
            JSONObject jsonObject = JSON.parseObject(headerstr);
            for(Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                headmap.put(entry.getKey(),String.valueOf(entry.getValue()));
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return headmap;
    }

    public static String getGetParam(Map<String,String> parammap){
        if(parammap == null || parammap.isEmpty()){
            return "";
        }
        StringBuffer buf=new StringBuffer();
        for(String parkey:parammap.keySet()){
            buf.append(parkey).append("=").append(parammap.get(parkey)).append("&");
        }
        return buf.substring(0,buf.length()-1);
    }

    public static void main(String[] args) {
        String str="{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true}";
        //System.out.println(getHeaderMap(str));
        String one="_ga=GA1.2.1147357397.1572676842; UM_distinctid=174d885696f1dd-02e275e11ef5ce-316b7004-13c680-174d8856970c7; CNZZDATA1279248237=1558269697-1601357548-https%253A%252F%252Fwww.baidu.com%252F%7C1601357548; Hm_lvt_39b794a97f47c65b6b2e4e1741dcba38=1601366850,1601371921,1601372296,1601372753; __gads=ID=6e6dee68a47d6ef0:T=1602152493:S=ALNI_MYS07yUN3VkmbdMD2xetIQQ7VDp3Q; CNZZDATA1277671799=1518513225-1603163158-https%253A%252F%252Fwww.baidu.com%252F%7C1603163158; Hm_lvt_fa5cea7cd463480c76a420af9fa8973e=1606475783; Hm_lvt_de25eb9ee20460fa7031c97a45632176=1607269317; CNZZDATA3347352=cnzz_eid%3D172947790-1607427842-https%253A%252F%252Fwww.baidu.com%252F%26ntime%3D1607433321; _gid=GA1.2.532254850.1608303782";
        Map<String,String> onemap=getCookievaluetoMap(one);
        System.out.println(getGetParam(onemap));

    }


}
